package cn.edu.scau.cmi.zenglinghong.client;

import java.util.List;
import java.util.ArrayList;
import cn.edu.scau.cmi.zenglinghong.adapter.Student;
import cn.edu.scau.cmi.zenglinghong.adapter.classAdapter;
import cn.edu.scau.cmi.zenglinghong.adapter.objectAdapter;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.layout.VBox;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Label;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.application.Application;

public class AdapterClient extends Application{
	public static void main(String[] args) {
		launch(args);
	}
	
	public static void init(Stage primaryStage) throws Exception {
		// TODO Auto-generated method stub
		VBox root = new VBox();
		TextArea textArea = new TextArea();
		TextArea output = new TextArea();
		output.setEditable(false);
		Button button1 = new Button();
		Button button2 = new Button();
		button1.setText("模式切换");
		button2.setText("排序并显示结果");
		Label label1 = new Label();
		Label label2 = new Label();
		label1.setText("类适配器");
		label2.setText("请输入学生的学号和姓名，并以单个空格分隔开，每行一个学生信息");
		button1.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				if ("类适配器".compareTo(label1.getText())==0) {
					label1.setText("对象适配器");
				} else {
					label1.setText("类适配器");
				}
			}
		});
		button2.setOnAction(new EventHandler<ActionEvent>() {
			@Override
			public void handle(ActionEvent event) {
				List<Student> studentList = new ArrayList<Student>();
				String[] input = textArea.getText().split("\n");
				try {
					for (String str: input) {
						String[] stuInformation = str.split(" ");
						Integer id = Integer.valueOf(stuInformation[0]);
						Student student = new Student(id, stuInformation[1]);
						studentList.add(student);
					}
					if (label1.getText().compareTo("类适配器")==0) {
						classAdapter adapter = new classAdapter();
						studentList=adapter.sortStudent(studentList);
					} else {
						objectAdapter adapter = new objectAdapter();
						studentList=adapter.sortStudent(studentList);
					}
					StringBuilder sb = new StringBuilder();
					for (Student str: studentList) {
						sb.append(str.id);
						sb.append(" ");
						sb.append(str.name);
						sb.append("\n");
					}
					output.setText(sb.toString());
				} catch (Exception e) {
					output.setText("输入格式错误");
					}
					
				}
				
			});

		primaryStage.setTitle("适配器模式");
		primaryStage.setHeight(600);
		primaryStage.setWidth(500);
		textArea.setMaxSize(480, 240);
		output.setMaxSize(480, 240);
		root.setSpacing(20);
		root.getChildren().add(button1);
		root.getChildren().add(label1);
		root.getChildren().add(label2);
		root.getChildren().add(textArea);
		root.getChildren().add(button2);
		root.getChildren().add(output);
		Scene scene = new Scene(root);
		primaryStage.setScene(scene);
		primaryStage.show();
	}
	
	@Override
	public void start(Stage primaryStage) throws Exception {
		init(primaryStage);
	}
}
